# coding=utf-8
import serial
import threading
import serial.tools.list_ports
#pip insatll pyserial
#ser=serial.Serial("/dev/ttyUSB0",9600,timeout=0.5) #使用USB连接串行口
#ser.write("123")
#ser=serial.Serial("COM31",9600,timeout=0.5)
#ser.read().encode("hex")
#int(ser.read().encode("hex"),16)
#ser2=serial.Serial("COM32",9600, parity='E',timeout=0.5)
#https://www.cnblogs.com/dongxiaodong/p/9992083.html
#https://blog.csdn.net/huayucong/article/details/48729907
#进制 转换 https://www.cnblogs.com/renke123/p/11029906.html
#查看port https://www.jianshu.com/p/2d4ab43cd345

#def hextostr(int_tmp):
#    hexstr=""
#    for item in list:
#        temp=hex(item)
#        if(len(temp)==3):
#            hexstr=hexstr+"0"+temp[2]
#        else:
#            hexstr=hexstr+temp[2]+temp[3]
#    strsend=hexstr.decode("hex")
#    return strsend


#def serial_inttostr(int_tmp):
#    hexstr = ""
#    temp = hex(int_tmp)
#    if (len(temp) == 3):
#        hexstr = hexstr + "0" + temp[2]
#    else:
#        hexstr = hexstr + temp[2] + temp[3]
#    strsend = hexstr.decode("hex")
#    return strsend



def serial_get_all_port():
    port_list=[]
    ports = serial.tools.list_ports.comports()
    for p in ports:
        port_list.append(p.device)
    return port_list

class serial_one_port_object():
    def __init__(self, port_tmp, baudrate_tmp):
        try:
            self.serial_port=serial.Serial(port=port_tmp,baudrate=baudrate_tmp,parity='E',timeout=0.5)
        except:
            self.serial_port=None

    def check_serial_port(self):
        return self.serial_port

    def check_serial_port_close(self):
        if(self.serial_port==None):
            pass
        else:
            self.serial_port.close()
    def serial_inttostr(self,int_tmp):
        hexstr = ""
        temp = hex(int_tmp)
        if (len(temp) == 3):
            hexstr = hexstr + "0" + temp[2]
        else:
            hexstr = hexstr + temp[2] + temp[3]
        strsend = hexstr.decode("hex")
        return strsend

    def hextostr(self,int_tmp):
        hexstr = ""
        for item in list:
            temp = hex(item)
            if (len(temp) == 3):
                hexstr = hexstr + "0" + temp[2]
            else:
                hexstr = hexstr + temp[2] + temp[3]
        strsend = hexstr.decode("hex")
        return strsend

    def serial_data_check(self,data):
        all_data=0
        if (type(data) ==type([])):
            len_tmp=len(data)
            if ((data[0] == 0x68) and (data[7] == 0x68)and(data[len_tmp-1]==0x16)):

                for i in range(len_tmp-2):
                    all_data+=data[i]
                if ((all_data%256)== data[len_tmp-2] ):
                    print("serial_data_check return 1")
                    return 1
        return 0

    def serial_data_handle_get_address(self,data):
        if(type(data)==type([])):
            if(len(data)):
                return int(str(data[6])+str(data[5])+str(data[4])+str(data[3])+str(data[2])+str(data[1]))
        return 0

    def serial_data_handle_get_control_code(self,data):
        if(type(data)==type([])):
            if(len(data)):
                return data[8]
        return 0

    def serial_data_handle_get_length_content(self,data):
        list_tmp=[]
        if(type(data)==type([])):
            if(len(data)):
                list_tmp.append(data[9])
                list_tmp.append(data[10:10 + data[9]])
        return list_tmp

    def serial_data_handle_req_read_command(self,data,str_tmp):
        if(type(data)==type([])):
            pass

    def serial_data_read(self):
        list_tmp = []
        # 找68H头码
        data_chr_tmp=self.serial_port.read(1)
        if(len(data_chr_tmp)):
            data_tmp_int=int(data_chr_tmp.encode("hex"),16)
            if(data_tmp_int==0x68):

                list_tmp.append(data_tmp_int)
                # 找6位地址，和68H，控制码
                data_str_tmp=self.serial_port.read(8)
                if(len(data_str_tmp)==8):
                    data_tmp_int = int(data_str_tmp[6].encode("hex"), 16)
                    if (data_tmp_int == 0x68):
                        for i in range(8):
                            list_tmp.append(int(data_str_tmp[i].encode("hex"), 16))
                        # 数据域长度
                        data_chr_tmp = self.serial_port.read(1)
                        if (len(data_chr_tmp)):
                            data_tmp_int = int(data_chr_tmp.encode("hex"), 16)
                            list_tmp.append(data_tmp_int)

                            # 数据域
                            data_str_tmp=self.serial_port.read(data_tmp_int)
                            if(data_tmp_int==len(data_str_tmp)):
                                for i in range(data_tmp_int):
                                    list_tmp.append(int(data_str_tmp[i].encode("hex"), 16))
                                #校验码,结尾
                                data_str_tmp = self.serial_port.read(2)
                                if (2 == len(data_str_tmp)):
                                    for i in range(2):
                                        list_tmp.append(int(data_str_tmp[i].encode("hex"), 16))
                                    if(self.serial_data_check(list_tmp)):
                                        return list_tmp
        del(list_tmp)
        list_tmp=[]
        return list_tmp
if __name__ == '__main__':
    #serial_tmp=serial_open_port_check("COM32", 2400)
    #serial_data_read(serial_tmp)

    #serial_tmp=serial_function("COM32", 2400)
    #list_tmp=serial_tmp.serial_data_read()
    #if(len(list_tmp)):
    #    serial_tmp.serial_data_handle()
    #else:
    #    pass
    a=serial_get_all_port()
    #print((a[0]).encode('utf-8'))
    #print(len(a[0]))
    while 1:
        serial_tmp=serial_one_port_object("COM32", 2400)
        if(serial_tmp.check_serial_port()!=None):

            try:
                while 1:
                    list_tmp=serial_tmp.serial_data_read()
                    if(len(list_tmp)):
                        print(serial_tmp.serial_data_handle_get_address(list_tmp))
            except:

                del(serial_tmp)
        else:
            pass

